package it.lorenz.server.fk.business.mail;

import java.util.Collection;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.springframework.stereotype.Component;

@Component("mailManager")
public class ACMailManager implements IMailManager {

    private String hostName;
    private int smtpPort;
    private MailEncryption mailEncryption;
    private String username;
    private String password;
    private boolean debugEnabled;

    public ACMailManager(String hostName, int smtpPort) {
        this(hostName, smtpPort, "NONE", null, null);
    }

    public ACMailManager(String hostName, int smtpPort, String mailEncryption) {
        this(hostName, smtpPort, mailEncryption, null, null);
    }

    public ACMailManager(String hostName, int smtpPort, String mailEncryption,
       String username, String password) {
        this.hostName = hostName;
        this.smtpPort = smtpPort;
        this.mailEncryption = MailEncryption.valueOf(mailEncryption);
        this.hostName = hostName;
        this.username = username;
        this.password = password;
        this.debugEnabled = false;
    }

    @Override
    public void send(String from, String bounceEmail, Collection<String> tos,
       Collection<String> ccs, Collection<String> bccs, String subject,
       String message, Boolean htmlFormatted,
       Collection<Attachment> attachments) throws EmailException {
        MultiPartEmail email = null;
        if (htmlFormatted) {
            email = new HtmlEmail();
        } else {
            email = new MultiPartEmail();
        }

        email.setDebug(debugEnabled);

        email.setHostName(hostName);
        email.setSmtpPort(smtpPort);
        if (username != null && password != null) {
            email.setAuthentication(username, password);
        }

        switch (mailEncryption) {
            case SSL:
                email.setSSLOnConnect(Boolean.TRUE);
                break;
            case TLS:
                email.setStartTLSEnabled(Boolean.TRUE);
                break;
            default:
                break;
        }

        email.setFrom(from);
        if (bounceEmail != null) {
            email.setBounceAddress(bounceEmail);
        }

        for (String to : tos) {
            email.addTo(to);
        }
        if (ccs != null) {
            for (String cc : ccs) {
                email.addCc(cc);
            }
        }
        if (bccs != null) {
            for (String bcc : bccs) {
                email.addBcc(bcc);
            }
        }

        if (subject == null) {
            subject = "";
        }
        email.setSubject(subject);

        if (message == null) {
            message = "";
        }
        if (htmlFormatted != null && htmlFormatted) {
            ((HtmlEmail) email).setHtmlMsg(message);
        } else {
            email.setMsg(message);
        }

        if (attachments != null) {
            EmailAttachment emailAttachment;
            for (Attachment attachment : attachments) {
                emailAttachment = new EmailAttachment();
                emailAttachment.setDisposition(EmailAttachment.ATTACHMENT);
                if (attachment.getUrl() != null) {
                    emailAttachment.setURL(attachment.getUrl());
                } else {
                    emailAttachment.setPath(attachment.getPath());
                }
                emailAttachment.setName(attachment.getName());
                emailAttachment.setDescription(attachment.getDescription());

                email.attach(emailAttachment);
            }
        }

        email.send();
    }

    public void setDebugEnabled(boolean debugEnabled) {
        this.debugEnabled = debugEnabled;
    }
}
